define(function (require, exports, module) {
    var template = require("template");
    var Ajax = require("ajax");
    var layer = require("layer");
    require('validationJs');
    require('validationRule');
    require('validationCss');
    var login = function (callback) {
        this.init.apply(this, arguments);
    };
    login.prototype = {
        constructor: login,
        options: {},
        init: function (options) {
            var t = this;
            if($.cookie("account")){
                $("#account").text($.cookie("account")).val($.cookie("account"));
                $("#pwd").text($.cookie("password")).val($.cookie("password"));
                $("#dochecked .iconfont").html('&#xeeeb;').addClass('checked');
            }else{
                $("#account").text($.cookie("account")).val($.cookie("account"));
                $("#pwd").text('').val($.cookie(''));
            }
            t.bindClick();
        },
        bindClick:function(){
        	//记住密码
        	$("#dochecked .iconfont").on('click',function(){
        		if($(this).hasClass('checked')){
        			$(this).html('&#xeeea;').removeClass('checked');
        		}else{
        			$(this).html('&#xeeeb;').addClass('checked');
        		}
        	});
            $('#doLogin').on('click',function(){
            	var rememberMe='false';
            	if($("#dochecked .iconfont").hasClass('checked')){
            		rememberMe='true';
            	}else{
            		rememberMe='false';
            	}
        		if(rememberMe=="true") {
    				//如果你想在整个网站中访问这个cookie需要这样设置有效路径：path: '/'。
    				$.cookie("rememberMe1", "true", { expires: 7,path:'/'}); 
    				$.cookie("account", $("#account").val().trim(), { expires: 7,path:'/'}); 
    				$.cookie("password", $("#pwd").val(), { expires: 7,path:'/'}); 
	    		} else {
	    			$.cookie("rememberMe1",null,{ path: '/' });
	    			$.cookie("account",null,{ path: '/' });
	    			$.cookie("password",null,{ path: '/' });
	    		}
    
//            	var newLogin=null;
//            	if($.cookie("account") && $("#pwd").val()!='000000'){
//            		newLogin=true;
//            	}else{
//            		newLogin=null;
//            	}
            	
                $("#loginForm").validationEngine({
                    focusFirstField:true,
                    scroll:true,
                    showOneMessage:false,
                    promptPosition:"topRight:10,10",
                    addPromptClass:"formError-style-layout",
                    validationEventTrigger:"blur",
                    maxErrorsPerField:"1",
                });
                var isSuccess = $('#loginForm').validationEngine('validate');
                if(isSuccess==true){
                	var reg = /(?!^\d+$)(?!^[a-zA-Z]+$)[0-9a-zA-Z]{3,16}/;   
                     /*if(!reg.test($.trim($("#account").val())))  
                     {  
                         layer.msg('请填写正确的账户名');
                         return false;  
                     } */
                	var dataJson={
	            		 account:$("#account").val().trim(),
	            		 password:$("#pwd").val()//password:hex_md5($("#pwd").val())
	            		 //以下待添加
//    	            	 rememberMe1:rememberMe,
//    	            	 newLogin:newLogin
            		};
                	Ajax.ajax({
                        url: localhost+"login/login.do",
                        data: dataJson,
                        success: function (data) {
                            layer.msg(data.returnMessage);
                            $.cookie("userName",data.returnData.userName||data.returnData.userPhone||data.returnData.userAccount);
                            $.cookie("roleEnum",data.returnData.maxRole.roleEnum);
                            if(data.returnData.company.companyId&&data.returnData.company.companyId>0){
                            	$.cookie("companyName",data.returnData.company.companyName);
                            }else{
                            	$.cookie("companyName",null);
                            }
                            if(data.returnData.depart.departId&&data.returnData.depart.departId>0){
                            	$.cookie("departName",data.returnData.depart.departName);
                            }else{
                            	$.cookie("departName",null);
                            }
                            setTimeout(function(){
                                window.location.href=localhost+('views/admin.jsp#!admin/home');
                            },2000);
                        }
                    });
                }
            });
            $("#pwd").keyup(function(event){
                if($("#pwd").is(":focus")&&event.keyCode=="13"){
                    $("#doLogin").trigger("click");
                }
		    });
            $("#account").keyup(function(event){
                if($("#account").is(":focus")&&event.keyCode=="13"){
                    $("#doLogin").trigger("click");
                }
		    });
        },
    };
    module.exports = login;
});